package com.yiyaOvo.cloudDisk.utils;

import com.yiyaOvo.cloudDisk.pojo.FileItem;

import java.io.*;
import java.net.Socket;
import java.util.List;

import static com.yiyaOvo.cloudDisk.utils.FileUtils.*;

/**
 * User: yiyaOvo
 * Date: 2021/5/23
 * Time: 15:30
 * role: Network communication tool class used by the client
 */
public class NetworkUtils {

    /**
     * get files list
     * @return
     */
    public static List<FileItem> getFileItems () {
        // connection server
        try (Socket socket = new Socket(IP, PORT);
             // get IO stream
             DataInputStream inputStream = new DataInputStream(socket.getInputStream());
             DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream())) {
            // send query command
            outputStream.writeInt(TYPE_LIST);
            // accept the JSON send by the server
            String json = inputStream.readUTF();
            // parse json and return
            return FileUtils.jsonToList(json);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * upload file
     * @param path
     */
    public static void uploadFile (String path) {
        // connection server
        try (Socket socket = new Socket(IP, PORT);
             // get IO stream
             DataInputStream inputStream = new DataInputStream(socket.getInputStream());
             DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream())) {
            // send upload command
            outputStream.writeInt(TYPE_UPLOAD);
            File file = new File(path);
            if (!file.exists()) {
                outputStream.writeUTF("error");
                System.out.println("upload error!!!!!!");
                return;
            }
            String fileName = file.getName();
            // send file name
            outputStream.writeUTF(fileName);
            // convert the file into a file input stream and send it out as a output stream
            FileInputStream fileInputStream = new FileInputStream(path);
            FileUtils.io(fileInputStream, outputStream);
            System.out.println("upload completed!!");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * file download
     */
    public static void downloadFile (String fileName) {
        // connection server
        try (Socket socket = new Socket(IP, PORT);
             // get IO stream
             DataInputStream inputStream = new DataInputStream(socket.getInputStream());
             DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream())) {
            // send download command
            outputStream.writeInt(TYPE_DOWNLOAD);
            // send file name
            outputStream.writeUTF(fileName);
            String name = inputStream.readUTF();
            if ("error".equals(name)) {
                System.out.println("the file is not exist");
                return;
            }
            // determine whether the directory exists, create it if it does not exist
            File file = new File(DOWNLOAD_DIR);
            if (!file.exists()) {
                file.mkdirs();
            }
            // read server-side data, write to disk file
            FileOutputStream fileOutputStream = new FileOutputStream(DOWNLOAD_DIR + "\\" + fileName);
            FileUtils.io(inputStream, fileOutputStream);
            System.out.println("download completed!!!");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * delete file
     * @param fileName
     */
    public static void deleteFile(String fileName) {
        // connection server
        try (Socket socket = new Socket(IP, PORT);
             // get IO stream
             DataInputStream inputStream = new DataInputStream(socket.getInputStream());
             DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream())) {
            // send download command
            outputStream.writeInt(TYPE_DELETE);
            // send file name
            outputStream.writeUTF(fileName);
            String name = inputStream.readUTF();
            if ("error".equals(name)) {
                System.out.println("the file is not exist");
                return;
            }
            System.out.println("delete completed!!!");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
